@echo off

set env=%1%
set projectName=%2%
set today=%date:~0,4%%date:~5,2%%date:~8,2%
set publishFolder=C:\Publish
set rar="C:\Program Files\WinRAR\winrar.exe"
set zip="C:\Program Files\7-Zip\7z.exe"

echo "[Debug:info] projectName=%projectName%"
echo "[Debug:info] env=%env%"

set "deployPackagesFolder="
IF "%env%" == "Dev20" (
	set "deployPackagesFolder=\\121.196.104.20\DeployPackage"
)
IF "%env%" == "lufei" (
	set "deployPackagesFolder=\\192.168.80.1\DeployPackage"
	echo "[Debug:info] start connect deployPackagesFolder: %deployPackagesFolder%"
	net use \\192.168.80.1\DeployPackage "ly1016" /user:"lufei"
)
IF %ERRORLEVEL% NEQ 0 GOTO ERROR

IF "%deployPackagesFolder%" == "" (
	echo "[Error:info] not exists deployPackagesFolder: %deployPackagesFolder%"
    GOTO ERROR
)

cd /d %publishFolder%
set fileName=%projectName%_%today%.7z
set zipFilePath=.\zipPackages\%fileName%

echo "[Debug:info] start zip package for %projectName%"
%zip% a -r %zipFilePath% .\%projectName%\* -xr@C:\Jenkins\bats\exclude_folder.txt -x@C:\Jenkins\bats\exclude_file.txt
IF %ERRORLEVEL% NEQ 0 GOTO ERROR

If exist %deployPackagesFolder%\%fileName% (
    echo "[Debug:info] start delete previous package"
    del /q %deployPackagesFolder%\%fileName%
)
IF %ERRORLEVEL% NEQ 0 GOTO ERROR

echo "[Debug:info] start copy package to deploy package folder: %deployPackagesFolder%"
xcopy /c /y /i %zipFilePath% "%deployPackagesFolder%\"
IF %ERRORLEVEL% NEQ 0 GOTO ERROR

:OK
ECHO command success
EXIT 0

:ERROR
ECHO command failed
EXIT 1